package com.talent.jiwen.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.talent.jiwen.R;
import com.talent.jiwen.base.BaseActivity;
import com.talent.jiwen.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen.http.common.ApiFactory;
import com.talent.jiwen.http.common.HttpUtil;
import com.talent.jiwen.http.common.ProgressSubscriber;
import com.talent.jiwen.http.result.InvitedListResult;
import com.talent.jiwen.http.result.StudentInfoResult;
import com.talent.jiwen.util.Constant;
import com.talent.jiwen.util.ImageUtil;
import com.talent.jiwen.util.SPConstant;
import com.talent.jiwen.util.SPUtil;
import com.talent.jiwen.util.Validators;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observable;

public class SearchFriendActivity extends BaseActivity {
    @BindView(R.id.contentEt)
    EditText contentEt;
    @BindView(R.id.delIv)
    ImageView delIv;
    @BindView(R.id.cancelTv)
    TextView cancelTv;
    @BindView(R.id.noDataTv)
    TextView noDataTv;
    @BindView(R.id.userLayout)
    RelativeLayout userLayout;
    @BindView(R.id.headIv)
    ImageView headIv;
    @BindView(R.id.nameTv)
    TextView nameTv;
    @BindView(R.id.gradeTv)
    TextView gradeTv;
    @BindView(R.id.optionTv)
    TextView optionTv;

    private static final String PARAM_GROUP_ID = "param.group.id";
    private String groupId;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_search_friend;
    }

    @Override
    public String titleName() {
        return null;
    }

    @Override
    protected void initData() {
        setHeadVisibility(View.GONE);

        noDataTv.setVisibility(View.GONE);
        userLayout.setVisibility(View.GONE);

        groupId = getIntent().getStringExtra(PARAM_GROUP_ID);

        contentEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String content = contentEt.getText().toString();
                if (Validators.isEmpty(content)){
                    delIv.setVisibility(View.GONE);
                }else{
                    delIv.setVisibility(View.VISIBLE);
                    if(content.length() == 11){
                        if (content.equals(SPUtil.getUserPhone())){
                            showToast(getString(R.string.search_result_is_yourSelf));
                        }else{
                            searchStudentByPhone(content);
                        }
                    }

                }
            }
        });

    }

    @OnClick({R.id.delIv, R.id.cancelTv, R.id.optionTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.delIv:
                contentEt.setText("");
                break;
            case R.id.cancelTv:
                finish();
                break;
            case R.id.optionTv:
                break;
        }
    }

    private void searchStudentByPhone(String number){

        Map<String , String> map = new HashMap<>();
        map.put("groupId" , groupId);
        map.put("studentPhone" , number);

        Observable observable = ApiFactory.getEducationApiSingleton().searchStudentByPhone(SPUtil.stringOut(SPConstant.TOKEN) ,map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<StudentInfoResult>(this) {
            @Override
            protected void _onNext(final StudentInfoResult studentInfoResult) {
                if (null == studentInfoResult.getStudentInfo()){
                    noDataTv.setVisibility(View.VISIBLE);
                    userLayout.setVisibility(View.GONE);
                }else{
                    noDataTv.setVisibility(View.GONE);
                    userLayout.setVisibility(View.VISIBLE);

                    ImageUtil.loadCorner4Image(SearchFriendActivity.this , studentInfoResult.getStudentInfo().getStudentHeadImage() , headIv);
                    nameTv.setText(studentInfoResult.getStudentInfo().getStudentName());
//                    if (studentInfoResult.getStudentInfo().getGradeLevel() != 0){
//                        gradeTv.setVisibility(View.VISIBLE);
//                        gradeTv.setText(Constant.gradeMap.get(studentInfoResult.getStudentInfo().getGradeLevel()));
//                    }else{
//                        gradeTv.setVisibility(View.GONE);
//                    }

                    gradeTv.setVisibility(View.GONE);

                    switch (studentInfoResult.getStudentInfo().getStudentGroupStatus()){
                        case 0:
                        case 5:
                            optionTv.setText("添加");
                            optionTv.setBackgroundResource(R.drawable.bg_circle_theme);
                            optionTv.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    inviteStudentJoinGroup(studentInfoResult.getStudentInfo().getStudentId());
                                }
                            });
                            break;
                        case 1:
                            //已接收
                            optionTv.setText("已接受");
                            optionTv.setBackgroundResource(R.drawable.bg_circle_orange);
                            break;
                        case 2:
                            //已拒绝
                            optionTv.setText("已拒绝");
                            optionTv.setBackgroundResource(R.drawable.bg_circle_red);
                            break;
                        case 3:
                            //已失效
                            optionTv.setText("已失效");
                            optionTv.setBackgroundResource(R.drawable.bg_circle_gray);
                            break;
                        case 4:
                            //已邀请
                            optionTv.setText("已邀请");
                            optionTv.setBackgroundResource(R.drawable.bg_circle_gray);
                            break;

                    }

                }
            }

            @Override
            protected void _onError(String message) {
                showToast(message);
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject,true);
    }

    /**邀请加入群组
     *
     * @param inviteStudentId
     */
    private void inviteStudentJoinGroup(String inviteStudentId){
        Map<String , String> map = new HashMap<>();
        map.put("groupId" , groupId);
        map.put("studentId" , inviteStudentId);
        map.put("inviteStudentId" , SPUtil.stringOut(SPConstant.USER_ID));

        Observable observable = ApiFactory.getEducationApiSingleton().inviteStudentJoinGroup(SPUtil.stringOut(SPConstant.TOKEN) , map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<Void>(this) {
            @Override
            protected void _onNext(Void v) {
                finish();
                EventBus.getDefault().post(TogetherListActivity.EVENT_REFRESH_INVITE_LIST);
            }

            @Override
            protected void _onError(String message) {
                showToast(message);
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject,true);
    }

    /**启动该页面
     *
     * @param context
     * @param groupId
     */
    public static void startSearchFriendActivity(Context context , String groupId){
        Intent intent = new Intent();
        intent.setClass(context , SearchFriendActivity.class);
        intent.putExtra(PARAM_GROUP_ID , groupId);
        context.startActivity(intent);
    }
}
